import request from "@/utils/request";
import type { AxiosPromise } from "axios";
import type { FileInfo, FileBase64Req, FileModel } from "./types";
import { useUserStore } from "@/stores/modules/user";

class FileApi {
  baseApiUrl(): string {
    let currentBaseURL = request.defaults.baseURL as string;
    // 使用 slice() 方法
    if (currentBaseURL.slice(-1) === "/") {
      currentBaseURL = currentBaseURL.slice(0, -1);
    }
    return currentBaseURL;
  }

  /** 上传Url */
  uploadUrl(): string {
    return this.baseApiUrl() + "/v1/file/upload?tenant=" + useUserStore().user.tenant;
  }

  /** 上传文件 */
  uploadApi(file: File): AxiosPromise<FileModel> {
    const formData = new FormData();
    formData.append("file", file);
    return request({
      url: this.uploadUrl(),
      method: "post",
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data"
      }
    });
  }

  /** Authorization */
  authHeader(): Object {
    return { Authorization: "Bearer " + useUserStore().token };
  }

  /**
   * url 获取图片Full路径
   * @param type resize	等比例缩放,white	补白,trans	补透明,clip	裁剪
   */
  imageUrl(imageUrl: string, width: number = 0, height: number = 0, type: string = "clip"): string {
    return this.baseApiUrl() + `/v1/file/image/${type}/${width}/${height}/` + imageUrl;
  }

  /**
   * url 获取图片Full路径
   * @param type resize	等比例缩放,white	补白,trans	补透明,clip	裁剪
   */
  fileCompleteUrl(
    imageUrl: string,
    width: number = 0,
    height: number = 0,
    type: string = "clip"
  ): string {
    const baseApiUrl = this.baseApiUrl() || window.location.origin;
    return baseApiUrl + `/v1/file/image/${type}/${width}/${height}/` + imageUrl;
  }

  /**
   * Id 获取图片Full路径
   * @param type resize	等比例缩放,white	补白,trans	补透明,clip	裁剪
   */
  imageIdUrl(
    imageId: string,
    width: number = 0,
    height: number = 0,
    type: string = "clip"
  ): string {
    return (
      this.baseApiUrl() +
      "/v1/file/image_id/" +
      useUserStore().user.tenant +
      `/${type}/${width}/${height}/` +
      imageId
    );
  }

  /** url 获取文件Full路径 */
  fileUrl(fileUrl?: string): string {
    return this.baseApiUrl() + "/v1/file/path/" + fileUrl;
  }

  /** Id 获取文件Full路径 */
  fileIdUrl(fileId?: string): string {
    return this.baseApiUrl() + "/v1/file/id/" + useUserStore().user.tenant + fileId;
  }

  /** 文件是否存在 */
  existFileHex(file: File, sm3: string, sha256: string): AxiosPromise<FileInfo> {
    const tenant = useUserStore().user.tenant;
    const file_size = file.size;
    return request({
      url: `/v1/file/exist/${tenant}/${file_size}/${sm3}/${sha256}`,
      method: "get"
    });
  }
  /** 上传文件 */
  uploadBase64File(base64: FileBase64Req): AxiosPromise<FileInfo> {
    base64.tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/file/base64upload",
      method: "post",
      data: base64
    });
  }

  /** 上传文件 */
  uploadFileApi(file: File): AxiosPromise<FileInfo> {
    const formData = new FormData();
    formData.append("file", file);
    return request({
      url: "/api/v1/files",
      method: "post",
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data"
      }
    });
  }

  /** 删除文件 */
  deleteFileApi(filePath?: string) {
    return request({
      url: "/api/v1/files",
      method: "delete",
      params: { filePath: filePath }
    });
  }
}

const fileApi = new FileApi();

export { fileApi };
